package easy;

import java.util.ArrayDeque;

/**
 * 2810. 故障键盘
 * 创建时间：2024-04-01 09:27
 * 地址：https://leetcode.cn/problems/faulty-keyboard/description
 */
public class 故障键盘_2810 {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.finalString("String"));
    }

    static class Solution {
        public String finalString(String s) {
            StringBuilder r = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'i') {
                    r.reverse();
                } else {
                    r.append(s.charAt(i));
                }
            }
            return String.valueOf(r);
        }

        /**
         * 使用双端队列的方法
         */
        public String finalString2(String s) {
            ArrayDeque<Character> deque = new ArrayDeque<>();
            boolean tail = true;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'i') {
                    tail = !tail;
                } else if(tail) {
                    deque.addLast(s.charAt(i));
                } else {
                    deque.addFirst(s.charAt(i));
                }
            }
            StringBuilder sb = new StringBuilder();
            for (Character c : deque) {
                sb.append(c);
            }
            if (!tail) {
                sb.reverse();
            }
            return sb.toString();
        }
    }
}
